import { formatCount, convertCurrency } from '@/utils/common.js';
import { baseUrl } from "@/utils/config.js"

function refundViews(info) {
  var str = convertCurrency(info.receivableAmount)
  var moneyArray = []
  var index = 0
  var i = str.indexOf('整') != -1 ? 2 : 1
  for (var item of str) {
    moneyArray.push({
      type: 'text',
      text: item,
      css: {
        fontWeight: (index + i >= str.length || index % 2) ? '400' : 'bold',
        paddingLeft: '6rpx',
      }
    })
    index++
  }
  var detailViews = []
  var keys = [{ key: 'index', width: '44rpx' }, { key: 'name', width: '159rpx' }, { key: 'unit', width: '60rpx' }, { key: 'number', width: '60rpx' }, { key: 'unitPrice', width: '100rpx' }, { key: 'total', width: '100rpx' }]
  index = 0
  for (var item of info.expenseDetails) {
    for (var data of keys) {
      var view = {
        type: 'view',
        css: {
          width: data.width,
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center',
          'box-sizing': 'border-box',
          height: '34rpx',
          border: '0 solid #333333',
          borderBottomWidth: index == Math.max(4,info.expenseDetails.length - 1) ? '0' : '1rpx',
          borderRightWidth: '1rpx',
          'box-sizing': 'border-box',
        },
        views: [
          {
            type: 'text',
            text: data.key == 'index' ? index + 1 : item[data.key],
            css: {
              fontWeight: 'bold',
            }
          }
        ]
      }
      detailViews.push(view)
    }
    index++
  }
  if (info.expenseDetails.length < 5) {
    for (var i = 0; i < 5 - info.expenseDetails.length; i++) {
      for (var data of keys) {
        var view = {
          type: 'view',
          css: {
            width: data.width,
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            'box-sizing': 'border-box',
            height: '34rpx',
            border: '0 solid #333333',
            borderBottomWidth: index == Math.max(4,info.expenseDetails.length - 1) ? '0' : '1rpx',
            borderRightWidth: '1rpx',
            'box-sizing': 'border-box',
          },
          views: [
            {
              type: 'text',
              text: data.key == 'index' ? index + 1 : '',
            }
          ]
        }
        detailViews.push(view)
      }
      index++
    }
  }
  return {
    css: {
      backgroundColor: '#FFFFFF',
      width: '702rpx',
      padding: '15rpx',
      boxSizing: "border-box",
      fontSize: '14rpx',
      color: '#333333',
      position: 'relative',
      'line-height': '17rpx'
    },
    views: [
      // 标题
      {
        type: 'view',
        css: {
          display: 'flex',
          justifyContent: 'center',
          alignItems: 'center',
          flexDirection: 'column',
          fontWeight: 'bold',
          textAlign: 'center',
          marginBottom: '28rpx',
          position: 'relative',
          width: '654rpx'
        },
        views: [
          {
            type: 'view',
            css: {
              height: '34rpx',
              display: 'flex',
              justifyContent: 'center',
              alignItems: 'center',
              position: 'relative',
              fontSize: '22rpx',
              marginTop: '10rpx'
            },
            views: [
              {
                type: 'text',
                text: info.repairShopName,
              }]
          },
          {
            type: 'view',
            css: {
              height: '6rpx',
              color: 'transparent',
              border: '0 solid #333333',
              borderBottomWidth: '1rpx',
              borderTopWidth: '1rpx',
              'box-sizing': 'border-box',
              position: 'relative',
              fontSize: '22rpx',
            },
            views: [
              {
                type: 'text',
                text: info.repairShopName,
              }]
          },
          {
            type: 'view',
            css: {
              top: 0,
              left: 0,
              position: 'absolute',
            },
            views: [
              {
                type: 'image',
                src: info.extend.logo  ? (info.extend.logo += '?imageView2/2/h/84') : '',
                css: {
                  'flex-shrink': 0,
                  height: '56rpx',
                  // width: '56rpx',
                  marginLeft: '28rpx'
                }
              }
            ]
          }, {
            type: 'view',
            css: {
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'flex-end',
              top: '24rpx',
              right: 0,
              position: 'absolute',
              color: '#875A38'
            },
            views: [
              {
                type: 'text',
                text: info.number,
                css: {
                  fontWeight: 'bold'
                }
              }
            ]
          }
        ]
      },
      // top 行
      {
        type: 'view',
        css: {
          width: '100%',
          display: 'flex',
          alignItems: 'flex-start',
          'min-height': '26rpx',
          marginBottom: '6rpx'
        },
        views: [
          {
            type: 'view',
            css: {
              width: '220rpx',
              display: 'flex',
              alignItems: 'flex-start',
            },
            views: [
              {
                type: 'text',
                text: '客户：',
                css: {
                  'flex-shrink': 0
                }
              },
              {
                type: 'text',
                text: info.customer.name,
                css: {
                  fontWeight: 'bold',
                }
              }
            ]
          }, {
            type: 'view',
            css: {
              width: '220rpx',
              display: 'flex',
              alignItems: 'flex-start',
            },
            views: [
              {
                type: 'text',
                text: '时间：',
                css: {
                  'flex-shrink': 0
                }
              },
              {
                type: 'text',
                text: info.repairTime.split(' ')[0],
                css: {
                  fontWeight: 'bold',
                }
              }
            ]
          }, 
          {
            type: 'view',
            css: {
              width: '200rpx',
              display: 'flex',
              alignItems: 'flex-start',
            },
            views: [
              {
                type: 'text',
                text: '电话：',
                css: {
                  'line-height': '16rpx',
                }
              },
              {
                type: 'text',
                text: info.customer.phone,
                css: {
                  fontWeight: 'bold',
                  'line-height': '16rpx',
                }
              }
            ]
          }
        ]
      },
      {
        type: 'view',
        css: {
          width: '100%',
          display: 'flex',
          alignItems: 'flex-start',
          'min-height': '26rpx',
          marginBottom: '6rpx'
        },
        views: [
        {
            type: 'view',
            css: {
              display: 'flex',
              alignItems: 'flex-start',
              width: '620rpx',
            },
            views: [
              {
                type: 'text',
                text: '客户地址：',
                css: {
                  'line-height': '16rpx',
                  'flex-shrink': 0
                }
              },
              {
                type: 'text',
                text: info.address,
                css: {
                  width: '570rpx',
                  'flex-shrink': 0,
                  fontWeight: 'bold',
                  'line-height': '16rpx',
                }
              }
            ]
          }
        ]
      },
      // table
      {
        type: 'view',
        css: {
          border: '2rpx solid #333333',
          display: 'flex',
          alignItems: 'flex-start',
          justifyContent: 'space-between',
          marign: '4rpx 0 8rpx 0',
          width: '652rpx',
          'box-sizing': 'border-box',
          'flex-wrap': 'wrap'
        },
        views: [
          // table 左
          {
            type: 'view',
            css: {
              width: '524rpx',
              display: 'flex',
              alignItems: 'center',
              'flex-wrap': 'wrap',
              'flex-shrink': 0,
              justifyContent: 'space-between',
              'box-sizing': 'border-box',
            },
            views: [
              // 第一行
              {
                type: 'view',
                css: {
                  width: '44rpx',
                  display: 'flex',
                  alignItems: 'center',
                  justifyContent: 'center',
                  'box-sizing': 'border-box',
                  height: '34rpx',
                  border: '0 solid #333333',
                  borderBottomWidth: '1rpx',
                  borderRightWidth: '1rpx',
                  'box-sizing': 'border-box',
                },
                views: [
                  {
                    type: 'text',
                    text: '序号'
                  }
                ]
              },
              {
                type: 'view',
                css: {
                  width: '159rpx',
                  display: 'flex',
                  alignItems: 'center',
                  justifyContent: 'center',
                  'box-sizing': 'border-box',
                  height: '34rpx',
                  border: '0 solid #333333',
                  borderBottomWidth: '1rpx',
                  borderRightWidth: '1rpx',
                  'box-sizing': 'border-box',
                },
                views: [
                  {
                    type: 'text',
                    text: '商品/服务名称'
                  }
                ]
              },
              {
                type: 'view',
                css: {
                  width: '60rpx',
                  display: 'flex',
                  alignItems: 'center',
                  justifyContent: 'center',
                  'box-sizing': 'border-box',
                  height: '34rpx',
                  border: '0 solid #333333',
                  borderBottomWidth: '1rpx',
                  borderRightWidth: '1rpx',
                  'box-sizing': 'border-box',
                },
                views: [
                  {
                    type: 'text',
                    text: '单位'
                  }
                ]
              },
              {
                type: 'view',
                css: {
                  width: '60rpx',
                  display: 'flex',
                  alignItems: 'center',
                  justifyContent: 'center',
                  'box-sizing': 'border-box',
                  height: '34rpx',
                  border: '0 solid #333333',
                  borderBottomWidth: '1rpx',
                  borderRightWidth: '1rpx',
                  'box-sizing': 'border-box',
                },
                views: [
                  {
                    type: 'text',
                    text: '退货数量',
                    css: {
                      textAlign: 'center'
                    }
                  }
                ]
              },
              {
                type: 'view',
                css: {
                  width: '100rpx',
                  display: 'flex',
                  alignItems: 'center',
                  justifyContent: 'center',
                  'box-sizing': 'border-box',
                  height: '34rpx',
                  border: '0 solid #333333',
                  borderBottomWidth: '1rpx',
                  borderRightWidth: '1rpx',
                },
                views: [
                  {
                    type: 'text',
                    text: '退货单价'
                  }
                ]
              },
              {
                type: 'view',
                css: {
                  width: '100rpx',
                  display: 'flex',
                  alignItems: 'center',
                  justifyContent: 'center',
                  'flex-wrap': 'wrap',
                  'box-sizing': 'border-box',
                  height: '34rpx',
                  border: '0 solid #333333',
                  borderBottomWidth: '1rpx',
                  borderRightWidth: '1rpx',
                },
                views: [
                  {
                    type: 'text',
                    text: '退货金额'
                  },
                ]
              },
              // 数据行
              ...detailViews,
            ]
          },
          // table 右
          {
            type: 'view',
            css: {
              width: '124rpx',
              display: 'flex',
              'flex-wrap': 'wrap',
              'flex-shrink': 0,
              flex: 1,
              'box-sizing': 'border-box',
            },
            views: [
              {
                type: 'view',
                css: {
                  width: '124rpx',
                  display: 'flex',
                  justifyContent: 'center',
                  alignItems: 'center',
                  height: '34rpx',
                  border: '0 solid #333333',
                  borderBottomWidth: '1rpx',
                  'box-sizing': 'border-box',
                },
                views: [
                  {
                    type: 'text',
                    text: '备注/说明'
                  }
                ]
              },
              {
                type: 'view',
                css: {
                  width: '100%',
                  display: 'flex',
                  justifyContent: 'center',
                  height: '34rpx',
                  padding: '8rpx',
                  'box-sizing': 'border-box',
                },
                views: [
                  {
                    type: 'text',
                    text: info.remark || ' ',
                    css: {
                      'line-height': '18rpx',
                      fontWeight: 'bold',
                    }
                  }
                ]
              }
            ]
          },
          // 合计行
          {
            type: 'view',
            css: {
              border: '0rpx solid #333333',
              display: 'flex',
              alignItems: 'flex-start',
              justifyContent: 'space-between',
              width: '650rpx',
              'box-sizing': 'border-box',
              border: '0 solid #333333',
              borderTopWidth: '1rpx',
            },
            views: [
              {
                type: 'view',
                css: {
                  width: '44rpx',
                  display: 'flex',
                  alignItems: 'center',
                  justifyContent: 'center',
                  'box-sizing': 'border-box',
                  height: '34rpx',
                  border: '0 solid #333333',
                  borderRightWidth: '1rpx',
                },
                views: [
                  {
                    type: 'text',
                    text: '合计',
                    css: {
                      textAlign: 'center'
                    }
                  }
                ]
              },
              {
                type: 'view',
                css: {
                  width: '606rpx',
                  display: 'flex',
                  alignItems: 'center',
                  'box-sizing': 'border-box',
                  height: '34rpx',
                },
                views: [
                  {
                    type: 'view',
                    css: {
                      display: 'flex',
                      alignItems: 'center',
                      justifyContent: 'flex-end',
                      'min-width': '80rpx',
                      height: '34rpx',
                    },
                    views: [
                      {
                        type: 'text',
                        text: '大写金额：',
                        css: {
                          textAlign: 'center'
                        }
                      }
                    ]
                  },
                  {
                    type: 'view',
                    css: {
                      display: 'flex',
                      alignItems: 'center',
                      width: '340rpx',
                      'box-sizing': 'border-box',
                      height: '34rpx',
                    },
                    views: [
                      ...moneyArray
                    ]
                  },
                  (info.extend.sealType == '1' || info.extend.sealType == '2') && info.extend.sealUrl ?
                    {
                      type: 'view',
                      css: {
                        width: '1rpx',
                        height: '34rpx',
                        display: 'flex',
                        alignItems: 'center',
                      },
                      views: [
                        {
                          type: 'image',
                          src: info.extend.sealUrl,
                          css: {
                            'flex-shrink': 0,
                            height: '96rpx',
                            marginLeft: info.extend.sealType == '2' ? '70rpx' : 0
                          }
                        }
                      ]
                    } : {},
                  {
                    type: 'view',
                    css: {
                      display: 'flex',
                      alignItems: 'center',
                      height: '34rpx',
                    },
                    views: [
                      {
                        type: 'text',
                        text: '(小写)',
                        css: {
                          textAlign: 'center',
                          'flex-shrink': 0
                        }
                      }
                    ]
                  },
                  {
                    type: 'view',
                    css: {
                      display: 'flex',
                      alignItems: 'center',
                      justifyContent: 'flex-start',
                      height: '34rpx',
                    },
                    views: [
                      {
                        type: 'text',
                        text: '￥' + formatCount(info.receivableAmount),
                        css: {
                          'white-space': 'nowrap',
                          fontWeight: 'bold',
                        }
                      }
                    ]
                  }
                ]
              },
            ]
          }
        ]
      },
      // 边
      {
        type: 'view',
        css: {
          width: '14rpx',
          position: 'absolute',
          top: '108rpx',
          right: '10rpx'
        },
        views: [
          {
            type: 'text',
            text: '本收据为电子凭证，具有同等效力',
            css: {
              'line-height': '15rpx',
              color: '#ED0C12',
              fontSize: '10rpx'
            }
          },
        ]
      },
      // bottom 行
      {
        type: 'view',
        css: {
          width: '100%',
          display: 'flex',
          alignItems: 'center',
          height: '34rpx',
          marginTop: '6rpx'
        },
        views: [
          {
            type: 'view',
            css: {
              width: '160rpx',
              display: 'flex',
              alignItems: 'center',
            },
            views: [
              {
                type: 'text',
                text: '经手人：',
                css: {
                  'flex-shrink': 0,
                }
              },
              {
                type: 'text',
                text: info.lister,
                css: {
                  fontWeight: 'bold',
                }
              }
            ]
          },
          {
            type: 'view',
            css: {
              width: '160rpx',
              display: 'flex',
              alignItems: 'center',
            },
            views: [
              {
                type: 'text',
                text: '应退金额：',
                css: {
                  'flex-shrink': 0,
                }
              },
              {
                type: 'text',
                text: '￥' + formatCount(info.receivableAmount),
                css: {
                  fontWeight: 'bold',
                }
              }
            ]
          }, {
            type: 'view',
            css: {
              width: '160rpx',
              display: 'flex',
              alignItems: 'center',
            },
            views: [
              {
                type: 'text',
                text: '实退金额：',
                css: {
                  'flex-shrink': 0,
                }
              },
              {
                type: 'text',
                text: '￥' + formatCount(info.actualAmount),
                css: {
                  fontWeight: 'bold',
                }
              }
            ]
          }, {
            type: 'view',
            css: {
              width: '180rpx',
              display: 'flex',
              alignItems: 'center',
            },
            views: [
              {
                type: 'text',
                text: '待退金额：',
                css: {
                  'flex-shrink': 0,
                }
              },
              {
                type: 'text',
                text: '￥' + formatCount(Math.max(Number(info.receivableAmount) - Number(info.actualAmount), 0)),
                css: {
                  fontWeight: 'bold',
                }
              }
            ]
          }
        ]
      },
      {
        type: 'view',
        css: {
          width: '100%',
          display: 'flex',
          alignItems: 'flex-start',
          height: '34rpx',
        },
        views: [
          {
            type: 'view',
            css: {
              width: '160rpx',
              display: 'flex',
              alignItems: 'center',
            },
            views: [
              {
                type: 'text',
                text: '电话：',
                css: {
                  'flex-shrink': 0,
                }
              },
              {
                type: 'text',
                text: info.phone,
                css: {
                  fontWeight: 'bold',
                }
              }
            ]
          },
          {
            type: 'view',
            css: {
              display: 'flex',
              alignItems: 'center',
              width: '320rpx'
            },
            views: [
              {
                type: 'text',
                text: '地址：',
                css: {
                  'flex-shrink': 0,
                }
              },
              {
                type: 'text',
                text: info.shopAddress,
                css: {
                  fontWeight: 'bold',
                  'line-height': '16rpx',
                }
              }
            ]
          },
          info.extend.customerSignSwitch ? {
            type: 'view',
            css: {
              width: '180rpx',
              display: 'flex',
              alignItems: 'flex-start',
              'flex-shrink': 0,
            },
            views: [
              {
                type: 'text',
                text: '客户签字：',
                css: {
                  'flex-shrink': 0,
                }
              },
              {
                type: 'image',
                src: info.extend.customerSign += '?imageView2/2/h/40',
                css: {
                  height: '24rpx'
                }
              }
            ]
          } : {
          },
        ]
      },
    ]
  }
}
export default refundViews